package vinh.game;

import utils.Polygon;
import android.graphics.Canvas;
import android.graphics.Paint;

public class PolygonSprite {

	protected Polygon shape; //at first
	protected Polygon sprite; // after interaction
	
	protected boolean active; 
	
	//current state
	double angle; //angle of the shape
	int x; //position
	int y;
	
	//modifications to execute
	double delta_angle; //rotation to execute
	double delta_x, delta_y; //displacement to execute
	
	
	/**
	 * Constructor
	 */
	public PolygonSprite(){
		this.shape = new Polygon();
		this.sprite = new Polygon();
		this.active = false;
	}
	
	public void render(){ 
		//create a shape to render it and draw it later.
		//it varies according to the position and the angle
		this.sprite = new Polygon();
		for (int i=0; i<this.shape.n(); i++){
			this.sprite.addPoint(
					(int) Math.round(
							this.shape.x_points()[i] * Math.cos(this.angle) 
							+ this.shape.y_points()[i] * Math.sin(this.angle))
						    + (int) Math.round(this.x) 
						    ,
				    (int) Math.round(
				    		this.shape.y_points()[i] * Math.cos(this.angle)
							- this.shape.x_points()[i] * Math.sin(this.angle))
						    + (int) Math.round(this.y)
						    );
		}
	}
	
	public boolean isColliding(PolygonSprite ps){
		//check if this polygon contains another one
		for(int i = 0; i < ps.sprite.n(); i++){
			if (this.sprite.contains(ps.sprite.x_points()[i], ps.sprite.y_points()[i])){
				return true;
			}
		}
		return false;
	}
	
	public void advance(int width, int height, double speed){
		//apply changes to the angle and the position
		this.angle += this.delta_angle;
		if (this.angle < 0)
			this.angle += 2*Math.PI;
		if (this.angle > 2 * Math.PI)
			this.angle -= 2 * Math.PI;
		
		this.x += this.delta_x;
		
		this.y -= this.delta_y;
		
	}
	
	void draw(Canvas canvas, Paint paint){
		//draw on the canvas in parameters
		float[] points = sprite.getPoints();
		if(points != null){
			canvas.drawLines(points, paint);
		}
	}
	
	
	public void set_position(double angle, int x, int y){
		this.angle = angle;
		this.x = x;
		this.y = y;
	}
	
	public void set_movement(double delta_angle, double delta_x, double delta_y){
		this.delta_angle = delta_angle;
		this.delta_x = delta_x;
		this.delta_y = delta_y;
	}
	
}
